//
//  BasicViewController.m
//  tristana
//
//  Created by 李康 on 2021/1/8.
//  Copyright © 2021 李康. All rights reserved.
//

#import "BasicViewController.h"
#import<WebKit/WebKit.h>
#import "TrisTanadManager.h"

//字符串是否为空
#define IsStrEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) || (![(_ref) isKindOfClass:[NSString class]]) ||([(_ref)isEqualToString:@""]))


@interface BasicViewController ()<WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *mainWebView;
@property (nonatomic, strong) UIProgressView *trLoadProgress;

@end

@implementation BasicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    self.mainWebView  = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];
    [self.view addSubview:self.mainWebView];
    self.mainWebView.navigationDelegate = self;
    NSURLRequest *request =  [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0f];
    [self.mainWebView loadRequest:request];
    [self.mainWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    self.edgesForExtendedLayout = UIRectEdgeNone;
     [self.view addSubview:self.trLoadProgress];
}



-   (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
    
    NSString * separatorString = [[NSUserDefaults standardUserDefaults] valueForKey:TSeparator];
    if (![navigationAction.request.URL.scheme isEqualToString:@"http"] && ![navigationAction.request.URL.scheme isEqualToString:@"https"]) {
        
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:[NSDictionary new] completionHandler:nil];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }else if (!IsStrEmpty(separatorString)){
        NSArray *stringArray = [separatorString componentsSeparatedByString:TLineSeparator];
        for (NSString *key in stringArray) {
            if ([navigationAction.request.URL.absoluteString containsString:key]) {
                [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:[NSDictionary new] completionHandler:nil];
                decisionHandler(WKNavigationActionPolicyCancel);
                return;
            }
        }
        decisionHandler(WKNavigationActionPolicyAllow);
    }else{
        decisionHandler(WKNavigationActionPolicyAllow);
    }

        
}


#pragma mark - event response
// 计算wkWebView进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.mainWebView && [keyPath isEqualToString:@"estimatedProgress"]) {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        self.trLoadProgress.alpha = 1.0f;
        [self.trLoadProgress setProgress:newprogress animated:YES];
        if (newprogress >= 1.0f) {
            [UIView animateWithDuration:0.3f
                                  delay:0.3f
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                self.trLoadProgress.alpha = 0.0f;
            }
                             completion:^(BOOL finished) {
                [self.trLoadProgress setProgress:0 animated:NO];
            }];
        }
        
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


- (UIProgressView *)trLoadProgress
{
    if (_trLoadProgress == nil) {
        _trLoadProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 2)];
        _trLoadProgress.tintColor = [UIColor redColor];
        _trLoadProgress.trackTintColor = [UIColor whiteColor];
        [self.view addSubview:_trLoadProgress];
        [self.view bringSubviewToFront:_trLoadProgress];
    }
    
    return _trLoadProgress;
}


@end
